package com.easygem.controller.front;

import com.easygem.common.annotation.ControllerEndpoint;
import com.easygem.common.model.front.ClientType;
import com.easygem.common.model.front.HighSeasClient;
import com.easygem.common.model.front.Industry;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.front.HighSeasClientVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.service.FrontClientTypeService;
import com.easygem.front.service.HighSeasClientService;
import com.easygem.front.service.IndustryService;
import com.easygem.system.service.UserService;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 何维佳
 * @version 1.0
 * @className HighSeasClientController
 * @Description 公海管理
 * @date 2025/1/1 16:44
 */
@Api(tags = "公海管理")
@RestController
@Transactional
@RequestMapping("/highSeasClient")
public class HighSeasClientController {
    @Autowired
    private HighSeasClientService highSeasClientService;
    @Autowired
    private IndustryService industryService;
    @Autowired
    private FrontClientTypeService clienttypeService;
    @Autowired
    private UserService usersService;

    //模糊查询所有公海资源
    @ApiOperation(value = "公海资源", notes = "模糊查询所有人")
    @GetMapping("/findHighSeasClient")
    @ResponseBody
    public ResponseBean<PageVO<HighSeasClient>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                                         HighSeasClientVO highSeasClientVO) {
        System.out.println("查询参数：" + highSeasClientVO);
        PageVO<HighSeasClient> highSeasClientPageVO = highSeasClientService.findList(pageNum, pageSize, highSeasClientVO);
        highSeasClientPageVO.setTotal(highSeasClientPageVO.getTotal());

        return ResponseBean.success(highSeasClientPageVO);
    }

    //行业列表
    @ApiOperation(value = "行业列表", notes = "模糊查询持有人")
    @GetMapping("/findIndustryVOList")
    @ResponseBody
    public List<Industry> findIndustryVOList(@RequestParam("accountName") String accountName) {
        List<Industry> industryList = industryService.findIndustryList(accountName);
        return industryList;
    }

    //客户种类列表
    @ApiOperation(value = "客户种类列表", notes = "模糊查询持有人")
    @GetMapping("/findClienttypeList")
    @ResponseBody
    public List<ClientType> findClienttypeList(@RequestParam("accountName") String accountName) {
        List<ClientType> clientTypeList = clienttypeService.findClienttypeList(accountName);
        return clientTypeList;
    }

    //创建人列表
    @ApiOperation(value = "创建人列表", notes = "模糊查询创建人")
    @GetMapping("/findCreateUsersList")
    @ResponseBody
    public List<User> findCreateUserList() {
        List<User> usersList = usersService.findCreateUsersList();
        return usersList;
    }

    //删除资源
    @ApiOperation(value = "删除资源", notes = "删除")
    @GetMapping("/deleteHighSeasClientById")
    @ResponseBody
    @RequiresPermissions("HigSeas_Client:delete")
    public int deleteHighSeasClientById(@RequestParam(value = "id") Integer id) {
        int result = highSeasClientService.delete(id);
        System.out.println(result);
        return result;
    }

    //转变为普通客户
    @ApiOperation(value = "转变为普通客户", notes = "转变")
    @GetMapping("/transform")
    @ResponseBody
    @RequiresPermissions("HigSeas_Client:transform")
    public Map<String, Integer> transform(@RequestParam(value = "id") Long id) {
        // 创建一个Map来存储各个操作的结果
        Map<String, Integer> resultMap = new HashMap<>();
        // 检查是否超出公海客户限制
//        int result = highSeasClientService.isExceedHighSeasClien();
//        resultMap.put("isExceedHighSeasClien", result);
//        if (result == 1) {
//            // 更新用户ID
//            int result1 = highSeasClientService.updateUserId(id);
//            resultMap.put("updateUserId", result1);
//            // 将Long类型的id转换为Integer类型
//            Integer integerId = id.intValue();
//            // 删除公海客户
//            int result2 = highSeasClientService.deleteHighSeasClientById(integerId);
//            resultMap.put("deleteHighSeasClientById", result2);
//            // 累计用户公海客户数量
//            int result3 = highSeasClientService.usersHighSeasCountAccumulate();
//            resultMap.put("usersHighSeasCountAccumulate", result3);
//
//            // 判断所有操作是否成功
//            if (result1 == 1 && result2 == 1 && result3 == 1) {
//                resultMap.put("overallResult", 1);
//            } else {
//                resultMap.put("overallResult", 0);
//            }
//        } else {
//            resultMap.put("overallResult", 0);
//            return resultMap;
//        }
        Integer integerId = id.intValue();
        highSeasClientService.deleteHighSeasClientById(integerId);
        resultMap.put("overallResult", 1);
        // 返回包含所有结果的Map
        return resultMap;
    }

    //导出excel
    @ApiOperation(value = "导出excel", notes = "导出所有用户的excel表格")
    @PostMapping("/excelAll")
    @RequiresPermissions("HigSeas_Client:excelAll")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败", operation = "导出用户excel")
    public void export(HttpServletResponse response) {
        List<HighSeasClient> highSeasClientList = this.highSeasClientService.findAll();
        System.out.println(highSeasClientList+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau ");
        ExcelKit.$Export(HighSeasClient.class, response).downXlsx(highSeasClientList, false);
    }

    //批量导出excel
    @ApiOperation(value = "批量导出excel", notes = "根据传入的id列表批量导出用户的excel表格")
    @PostMapping("/excelBatch")
    @RequiresPermissions("HigSeas_Client:export")
    @ControllerEndpoint(exceptionMessage = "批量导出Excel失败", operation = "批量导出用户excel")
    public void batchExport(HttpServletResponse response,
                            @RequestBody List<Long> ids) {
        List<HighSeasClient> highSeasClientList = this.highSeasClientService.findAllByIds(ids);
        ExcelKit.$Export(HighSeasClient.class, response).downXlsx(highSeasClientList, false);
    }

    //加入公海
    @ApiOperation(value = "加入公海", notes = "加入公海")
    @PostMapping("/toHighSeas")
    @RequiresPermissions({"customer:insertHighSeas"})
    @ResponseBody
    public ResponseBean<Integer> toHighSeas(@RequestBody Map<String, Long[]> request) {
        Long[] resourceIds = request.get("id");
        System.out.println("客户id：" + Arrays.toString(resourceIds));
        Integer result = highSeasClientService.toHighSeas(resourceIds);
        return ResponseBean.success(result);
    }
}
